package android.slc.box.core.controller;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;

import java.util.function.UnaryOperator;

/**
 * 一般的控制器
 *
 * @author slc
 * @date 2022/03/28 下午 5:58
 * 请使用{@link CustomTextFormatter}
 */
@Deprecated
public class GeneralTextFormatter extends TextFormatter<String> {

    public GeneralTextFormatter() {
        super(getDefUnaryOperator());
    }

    public GeneralTextFormatter(StringConverter<String> valueConverter, String defaultValue, UnaryOperator<Change> filter) {
        super(valueConverter, defaultValue, getDefUnaryOperator());
    }

    public GeneralTextFormatter(StringConverter<String> valueConverter, String defaultValue) {
        super(valueConverter, defaultValue);
    }

    public GeneralTextFormatter(StringConverter<String> valueConverter) {
        super(valueConverter);
    }

    private static UnaryOperator<Change> getDefUnaryOperator() {
        return new UnaryOperator<Change>() {
            @Override
            public Change apply(Change change) {
                if (change.isAdded()) {
                    String text = change.getText();
                    if (!ReUtil.isMatch(PatternPool.GENERAL, text)) {
                        return null;
                    }
                }
                return change;
            }
        };
    }
}
